<?php
require_once '../../configs/mysql_config.class.php';

class GestionBoutique {
    #region Champs Statiques

    /**Objet de la classe PDO
     * @var PDO
     */
    private static $pdoCnxBase = null;

    /**Objet de la classe PDOStatement
     * @var PDOStatement
     */
    private static $pdoStResults = null;

    private static $requete = ""; //text de la requête

    private static $resultat = null; //résultat de la requête
    #endregion

    #region Méthodes Statiques
        public static function seConnecter(){
            if (!isset(self::$pdoCnxBase)) {
                try {
                    self::$pdoCnxBase = new PDO('mysql:host=' . MysqlConfig::NOM_SERVEUR . ';dbname=' . MysqlConfig::NOM_BASE, MysqlConfig::NOM_UTILISATEUR, MysqlConfig::MOT_DE_PASSE);

                    self::$pdoCnxBase->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    self::$pdoCnxBase->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
                    self::$pdoCnxBase->query("SET CHARACTER SET utf8");

                } catch (Exception $e) {
                    echo 'Erreur : ' . $e->getMessage() . '<br />';
                    echo 'Code : ' . $e->getCode();
                }
            }
        }

        public static function seDeconnecter() {
            self::$pdoCnxBase = null;
        }

        public static function getLesCategories() {
            self::seConnecter();

            self::$requete = "SELECT * FROM Categorie";
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->execute();
            self::$resultat = self::$pdoStResults->fetchAll();

            self::$pdoStResults->closeCursor();

            return self::$resultat;
        }

        public static function getLesProduits() {
            self::seConnecter();

            self::$requete = "SELECT * FROM Produit";
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->execute();
            self::$resultat = self::$pdoStResults->fetchAll();

            self::$pdoStResults->closeCursor();

            return self::$resultat;
        }

        public static function getLesProduitsByCategorie($libelleCategorie) {
            self::seConnecter();

            self::$requete = "SELECT * FROM Produit p, Categorie c where P.idCategorie = c.id AND libelle = :libCateg";
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->bindValue('libCateg', $libelleCategorie);
            self::$pdoStResults->execute();
            self::$resultat = self::$pdoStResults->fetchAll();

            self::$pdoStResults->closeCursor();

            return self::$resultat;
        }

        public static function getProduitById($idProduit) {
            self::seConnecter();

            self::$requete = "SELECT * FROM Produit where id = :idProd";
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->bindValue('idProd', $idProduit);
            self::$pdoStResults->execute();
            self::$resultat = self::$pdoStResults->fetch();

            self::$pdoStResults->closeCursor();

            return self::$resultat;
        }

        public static function getNbProduits() {
            self::seConnecter();

            self::$requete = "SELECT COUNT(*) As nbProduits FROM Produit";
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);

            self::$pdoStResults->execute();
            self::$resultat = self::$pdoStResults->fetch();

            self::$pdoStResults->closeCursor();

            return self::$resultat->nbProduits;
        }

        public static function ajouterCategorie($libelleCateg) {
            self::seConnecter();

            self::$requete = "Insert into Categorie(libelle) values(:libelle)";
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->bindValue('libelle', $libelleCateg);
            self::$pdoStResults->execute();
        }

        public static function ajouterProduit($libelleProd, $idCateg) {
            self::seConnecter();

            self::$requete = "Insert into Produit(nom, idCategorie) values(:libelle, :idCat)";
            self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
            self::$pdoStResults->bindValue('libelle', $libelleProd);
            self::$pdoStResults->bindValue('idCat', $idCateg);
            self::$pdoStResults->execute();
        }

    #endregion
}

?>



